package com.zhenyi.youqu.admin.index;

import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.youqu.common.model.YqCategory;
import com.youqu.common.model.YqModel;
import com.youqu.common.model.YqModelInfo;
import com.youqu.common.model.YqProduct;
import com.youqu.common.model.YqShopUser;

/**
 * 模块管理
 * 
 * @author cen
 *
 */
public class ModelController extends YQAdminController {

	/**
	 * 商户自己的模块
	 */
	@Override
	public void list() {
		Long shopId = getParaToLong("shopId");
		YqShopUser user = getShopUser();
		Kv kv = Kv.create();
		if (!user.getRole().equals(YqShopUser.ROLE_SUPER_ADMIN)) {
			kv.set("appId", user.getAppId());
		}
		if (user.getRole().equals(YqShopUser.ROLE_SHOP)) {
			kv.set("shopId", user.getShopId());
		} else if (shopId != null) {
			kv.set("shopId", shopId);
			setAttr("shopId", shopId);
		}
		SqlPara sqlPara = db.getSqlPara("YQModels", kv);
		Page<Record> pages = db.paginate(getPage(), PAGE_SIZE, sqlPara);
		List<Record> list = pages.getList();
		setRainbow(pages);
		setData(list);
		render("/yq/admin/model/list.html");
	}

	/**
	 * 模块列表API
	 */
	public void models() {
		renderDevelop();
	}

	/**
	 * 模块详情,查看每个模块的内容,添加内容,连接,子模块
	 */
	@Override
	public void info() {
		setReferer();
		Long modelId = getParaToLong("modelId");
		YqShopUser user = getShopUser();
		YqModel model = YqModel.dao.findById(modelId);
		if (model == null) {
			model = new YqModel();
			if (user.getRole().equals(YqShopUser.ROLE_SHOP)) {
				model.setShopId(user.getShopId());
			}
		}
		setData(model);
		SqlPara sp = db.getSqlPara("YQShops", Kv.by("appId", user.getAppId()));
		List<Record> shops = db.find(sp);
		setAttr("shops", JsonKit.toJson(shops));
		render("/yq/admin/model/info.html");
	}

	/**
	 * 可以创造子模块
	 */
	@Override
	public void submit() {
		Long id = getParaToLong("id");
		Long shopId = getParaToLong("shopId");
		String title = getPara("title");
		String backgroundImage = getPara("backgroundImage");
		String cover = getPara("cover");
		String subtitle = getPara("subtitle");
		String shareImage = getPara("shareImage");
		String shareTitle = getPara("shareTitle");
		String url = getPara("url");
		String intro = getPara("intro");
		String html = getPara("html");
		Integer isHidden = getParaToInt("isHidden", 0);
		Integer showCount = getParaToInt("showCount", 5);
		String desc = getPara("desc");
		Integer type = getParaToInt("type", 1);
		String name = getPara("name");
		String themeColor = getPara("themeColor");
		String themeColor2 = getPara("themeColor2");
		Integer isMiniApp = getParaToInt("isMiniApp", 1);
		Integer isAPP = getParaToInt("isAPP", 1);
		Integer isDeleteable = getParaToInt("isDeleteable", 0);

		if (shopId == null) {
			renderFailure("shopId is null");
			return;
		}

		YqModel model;
		if (id == null) {
			model = new YqModel();
			model.setShopId(shopId);
			model.setIsHidden(isHidden);
			model.setShowCount(showCount);
			model.setType(type);
			model.setIsMiniApp(isMiniApp);
			model.setIsAPP(isAPP);
			model.setIsDeleteable(isDeleteable);
			model.save();
		} else {
			model = YqModel.dao.findById(id);
			if (model == null) {
				renderError(404);
				return;
			}
		}
		model.setShopId(shopId);
		model.setTitle(title);
		model.setBackgroundImage(backgroundImage);
		model.setCover(cover);
		model.setSubtitle(subtitle);
		model.setShareImage(shareImage);
		model.setShareTitle(shareTitle);
		model.setUrl(url);
		model.setIntro(intro);
		model.setHtml(html);
		model.setIsHidden(isHidden);
		model.setShowCount(showCount);
		model.setDesc(desc);
		model.setType(type);
		model.setName(name);
		model.setThemeColor(themeColor);
		model.setThemeColor2(themeColor2);
		model.setIsMiniApp(isMiniApp);
		model.setIsAPP(isAPP);
		model.setIsDeleteable(isDeleteable);
		renderFlag(model.update());
	}

	/**
	 * ModelInfo list
	 */
	public void modelInfoList() {
		Long modelId = getParaToLong("modelId");
		YqModel model = YqModel.dao.findById(modelId);
		if (model == null) {
			renderError(404);
			return;
		}
		Kv kv = Kv.by("modelId", modelId);
		SqlPara sqlPara = db.getSqlPara("YQModelInfos", kv);
		Page<Record> pages = db.paginate(getPage(), PAGE_SIZE, sqlPara);
		List<Record> list = pages.getList();
		setAttr("model", model);
		setRainbow(pages);
		setData(list);
		render("/yq/admin/model/modelInfoList.html");
	}

	public void modelInfoItem() {
		setReferer();
		Long modelId = getParaToLong("modelId");
		Long modelInfoId = getParaToLong("modelInfoId");
		YqShopUser user = getShopUser();
		if (modelInfoId == null && modelId == null) {
			renderFailure("model id is required");
			return;
		}
		YqModelInfo modelInfo = YqModelInfo.dao.findById(modelInfoId);
		if (modelInfo == null) {
			modelInfo = new YqModelInfo();
			modelInfo.setModelId(modelId);
			modelInfo.setIsShow(1);
		}
		SqlPara spProducts;
		SqlPara spModels;
		SqlPara spCategories;
		if (user.getRole().equals(YqShopUser.ROLE_SHOP)) {
			spProducts = db.getSqlPara("YQProducts", Kv.by("shopId", user.getShopId()));
			spModels = db.getSqlPara("YQModels", Kv.by("shopId", user.getShopId()));
			spCategories = db.getSqlPara("YQAdminCategories", Kv.by("shopId", user.getShopId()));
		} else {
			spProducts = db.getSqlPara("YQProducts");
			spModels = db.getSqlPara("YQModels");
			spCategories = db.getSqlPara("YQAdminCategories");
		}
		List<YqProduct> products = YqProduct.dao.find(spProducts);
		setData("products", products);
		List<YqModel> models = YqModel.dao.find(spModels);
		setData("models", models);
		List<YqCategory> categories = YqCategory.dao.find(spCategories);
		setData("categories", categories);
		setData(modelInfo);
		render("/yq/admin/model/modelInfoItem.html");
	}

	public void submitModelInfo() {
		Long id = getParaToLong("id");
		Long modelId = getParaToLong("modelId");
		Long productId = getParaToLong("productId");
		String url = getPara("url");
		String cover = getPara("cover");
		String title = getPara("title");
		String subtitle = getPara("subtitle");
		Integer isShow = getParaToInt("isShow", 1);
		Long subModelId = getParaToLong("subModelId");
		Integer sort = getParaToInt("sort", 99);
		String extra = getPara("extra");
		Long categoryId = getParaToLong("categoryId");

		if (modelId == null) {
			renderFailure("modelId is required");
			return;
		}
		if (productId == null && subModelId == null && categoryId == null) {
			renderFailure("productId/subModelId/categoryId is required");
			return;
		}

		YqModelInfo modelInfo;
		if (id == null) {
			modelInfo = new YqModelInfo();
			modelInfo.setModelId(modelId);
			modelInfo.setIsShow(isShow);
			modelInfo.setSort(sort);
			modelInfo.save();
		} else {
			modelInfo = YqModelInfo.dao.findById(id);
			if (modelInfo == null) {
				renderError(404);
				return;
			}
		}
		if (productId != null) {
			modelInfo.setProductId(productId);
			modelInfo.setSubModelId(null);
			modelInfo.setCategoryId(null);
		} else if (subModelId != null) {
			modelInfo.setSubModelId(subModelId);
			modelInfo.setProductId(null);
			modelInfo.setCategoryId(null);
		} else if (categoryId != null) {
			modelInfo.setCategoryId(categoryId);
			modelInfo.setSubModelId(null);
			modelInfo.setProductId(null);
		}
		modelInfo.setModelId(modelId);
		modelInfo.setUrl(url);
		modelInfo.setCover(cover);
		modelInfo.setTitle(title);
		modelInfo.setSubtitle(subtitle);
		modelInfo.setIsShow(isShow);
		modelInfo.setSort(sort);
		modelInfo.setExtra(extra);
		renderFlag(modelInfo.update());
	}
}
